﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Linq;
using System.ServiceModel.Configuration;
using System.Text;
using System.Windows.Forms;
using VNet.VisiSuite.Common.Resource;
using VNet.VisiSuite.Common.Security;
using VNet.VisiSuite.Service.Contact.Message;
using VNet.VisiSuite.Service.Contact.Message.Base;
using VNet.VisiSuite.Service.Contact.Message.Security;
using VNet.VisiSuite.Service.Proxy;
using VNet.VisiSuite.VisiCentral.Models;


namespace VNet.VisiSuite.VisiCentral.Providers
{
    public class VisiCentralProvider
    {

        public static void GetCells(object sender, DoWorkEventArgs e)
        {
            var worker = sender as BackgroundWorker;
            e.Result = null;
            if (worker != null && worker.CancellationPending == false)
            {
                if (worker != null && worker.CancellationPending == false)
                {
                    var client = new VNetVisiSuiteContractClient();
                    var request = new GetDataRequest
                    {
                        Identity = UserTransportToken.UserIdentity,
                        Id = (int)e.Argument,
                    };
                    e.Result = client.GetCells(request);
                }
            }
        }

        public static void GetReasonCodes(object sender, DoWorkEventArgs e)
        {
            var worker = sender as BackgroundWorker;
            e.Result = null;
            if (worker != null && worker.CancellationPending == false)
            {
                if (worker != null && worker.CancellationPending == false)
                {
                    var client = new VNetVisiSuiteContractClient();
                    var request = new GetDataRequest
                    {
                        Identity = UserTransportToken.UserIdentity,
                        Id = (int)e.Argument,
                    };
                    e.Result = client.GetReasonCodes(request);
                }
            }
        }

        public static void GetScrapReasonCodes(object sender, DoWorkEventArgs e)
        {
            var worker = sender as BackgroundWorker;
            e.Result = null;
            if (worker != null && worker.CancellationPending == false)
            {
                if (worker != null && worker.CancellationPending == false)
                {
                    var client = new VNetVisiSuiteContractClient();
                    var request = new GetDataRequest
                    {
                        Identity = UserTransportToken.UserIdentity,
                        Id = (int)e.Argument,
                    };
                    e.Result = client.GetScrapReasonCodes(request);
                }
            }
        }

        public static void GetAreaShifts(object sender, DoWorkEventArgs e)
        {
            var worker = sender as BackgroundWorker;
            e.Result = null;
            if (worker != null && worker.CancellationPending == false)
            {
                if (worker != null && worker.CancellationPending == false)
                {
                    var client = new VNetVisiSuiteContractClient();
                    var request = new GetDataRequest
                    {
                        Identity = UserTransportToken.UserIdentity,
                        Id = (int)e.Argument,
                    };
                    e.Result = client.GetAreaShifts(request);
                }
            }
        }

        public static void GetProducts(object sender, DoWorkEventArgs e)
        {
            var worker = sender as BackgroundWorker;
            e.Result = null;
            if (worker != null && worker.CancellationPending == false)
            {
                if (worker != null && worker.CancellationPending == false)
                {
                    var client = new VNetVisiSuiteContractClient();
                    var request = new GetDataRequest
                    {
                        Identity = UserTransportToken.UserIdentity
                    };
                    e.Result = client.GetProducts(request);
                }
            }
        }

        public static void GetAnalysisChartData(object sender, DoWorkEventArgs e)
        {
            var worker = sender as BackgroundWorker;
            e.Result = null;
            if (worker != null && worker.CancellationPending == false)
            {
                if (worker != null && worker.CancellationPending == false)
                {
                    var argument = e.Argument as AnalysisChartDataArgument;

                    if (argument != null)
                    {
                        var client = new VNetVisiSuiteContractClient();
                        var request = new AnalysisChartDataRequest
                        {
                            Identity = UserTransportToken.UserIdentity,
                            DisplayScrapReasonCode = argument.DisplayScrapReasonCode,
                            FromDate = argument.FromDate,
                            ToDate = argument.ToDate,
                            IsAllAreaShiftIdSelected = argument.IsAllAreaShiftIdSelected,
                            IsAllCellIdSelected = argument.IsAllCellIdSelected,
                            IsAllProductIdSelected = argument.IsAllProductIdSelected,
                            IsAllReasonCodeIdSelected = argument.IsAllReasonCodeIdSelected,
                            ListAreaShiftId = argument.ListAreaShiftId,
                            ListCellId = argument.ListCellId,
                            ListProductId = argument.ListProductId,
                            ListReasonCodeId = argument.ListReasonCodeId,
                            SeparateOut = argument.SeparateOut,
                            TopDowntimeBottom = argument.TopDowntimeBottom,
                            TopDowntimeBottomBy = argument.TopDowntimeBottomBy,
                            TopDowntimeLeft = argument.TopDowntimeLeft,
                            TopDowntimeLeftBy = argument.TopDowntimeLeftBy,
                            TopDowntimeRight = argument.TopDowntimeRight,
                            TopDowntimeRightBy = argument.TopDowntimeRightBy
                        };
                        e.Result = client.GetAnalysisChartData(request);
                    }
                }
            }
        }

        public class CellProductionData
        {
            public int AreaId { get; set; }
            public int CellId { get; set; }
            public int AreaShiftId { get; set; }
            public DateTime ShiftDate { get; set; }
        }

        public static void GetCellProductionData(object sender, DoWorkEventArgs e)
        {
            var worker = sender as BackgroundWorker;
            var input = e.Argument as CellProductionData;
            e.Result = null;
            if (worker != null && worker.CancellationPending == false && input != null)
            {
                    var client = new VNetVisiSuiteContractClient();
                    var request = new ProductionDataRequest
                    {
                        Identity = UserTransportToken.UserIdentity,
                        AreaId = input.AreaId,
                        CellId = input.CellId,
                        AreaShiftId = input.AreaShiftId,
                        ShiftDate = input.ShiftDate
                    };
                    e.Result = client.GetProductionData(request);
            }
        }

    }
}
